package com.jcrj.service;

import com.jcrj.dao.FormDao;
import com.jcrj.dao.HolidayDao;
import com.jcrj.dao.TypeSetDao;
import com.jcrj.dao.WorkflowManagerDao;
import com.jcrj.entity.OAuthority;
import com.jcrj.entity.ODepartment;
import com.jcrj.entity.OFlowForm;
import com.jcrj.entity.OFlowFormProporty;
import com.jcrj.entity.OFlowType;
import com.jcrj.entity.OForm;
import com.jcrj.entity.OFormColumn;
import com.jcrj.entity.OFormDefinition;
import com.jcrj.entity.OFormNode;
import com.jcrj.entity.OFormProporty;
import com.jcrj.entity.OFormType;
import com.jcrj.entity.ONode;
import com.jcrj.entity.ONodeTransaction;
import com.jcrj.entity.OOrg;
import com.jcrj.entity.OProcessHis;
import com.jcrj.entity.OProcessinstance;
import com.jcrj.entity.ORole;
import com.jcrj.entity.OTaskActor;
import com.jcrj.entity.OTypeSet;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkflow;
import com.jcrj.entity.Subject;
import com.jcrj.entity.SubjectOrg;
import com.jcrj.entity.TemplateFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unchecked")
public class WorkflowManagerService {
	private WorkflowManagerDao workflowManagerDao;
	private FormDao formDao;
	private FlowFormService flowFormService;
	private TypeSetDao typeSetDao;
	private MailService mailService;
	private MessageService messageService;
	private HolidayDao holidayDao;

	public HolidayDao getHolidayDao() {
		return this.holidayDao;
	}

	public void setHolidayDao(HolidayDao holidayDao) {
		this.holidayDao = holidayDao;
	}

	public MessageService getMessageService() {
		return this.messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public TypeSetDao getTypeSetDao() {
		return this.typeSetDao;
	}

	public void setTypeSetDao(TypeSetDao typeSetDao) {
		this.typeSetDao = typeSetDao;
	}

	public List<ODepartment> getDepartments(int orgId) {
		List list = getOrgAndParent(orgId);
		String hql = " from OTypeSet t where t.id in(select typeId from ODepartment where departId in(:list))";
		return this.typeSetDao.findListByName(hql, new String[] { "list" }, new Object[] { list });
	}

	public List<OTypeSet> getTypes(String name, int page) {
		String hql = "from OTypeSet o where o.del='0'";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and  name like '%" + name.trim() + "%'";
		}
		hql = hql + " order by o.createTime desc,o.id desc";
		return this.workflowManagerDao.findByPage(hql, page);
	}

	public long getTypeCount(String name) {
		String hql = "select count(id) from OTypeSet o where o.del='0'";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and  name like '%" + name.trim() + "%'";
		}
		return this.workflowManagerDao.countTotalByHql(hql).longValue();
	}

	public void saveType(OTypeSet typeSet) {
		this.typeSetDao.saveType(typeSet);
		if ((typeSet.getDepartmentIds() != null) && (!"".equals(typeSet.getDepartmentIds()))) {
			String[] ids = typeSet.getDepartmentIds().split(",");
			for (int i = 0; i < ids.length; i++) {
				ODepartment department = new ODepartment();
				department.setDepartId(Integer.valueOf(ids[i]));
				department.setTypeId(typeSet.getId());
				this.typeSetDao.saveDepartment(department);
			}
		}
	}

	public List<OFlowType> getFlowTypeList(List<OWorkflow> workList) {
		StringBuffer id = new StringBuffer();
		for (int i = 0; i < workList.size(); i++) {
			if (id.length() == 0)
				id.append(((OWorkflow) workList.get(i)).getTypeId());
			else {
				id.append("," + ((OWorkflow) workList.get(i)).getTypeId());
			}
		}
		String hql = "from OFlowType where id in(select t.typeId from OWorkflow t where t.typeId in(" + id.toString() + "))";
		return this.workflowManagerDao.findByHql(hql);
	}

	public OTypeSet updateType(OTypeSet typeSet) {
		return (OTypeSet) this.typeSetDao.getObject(typeSet, typeSet.getId().intValue());
	}

	public void updateTypeSave(OTypeSet typeSet) {
		String hql = "delete from ODepartment where typeId=" + typeSet.getId();
		this.typeSetDao.executeHql(hql);
		this.typeSetDao.saveType(typeSet);
		String[] ids = typeSet.getDepartmentIds().split(",");
		for (int i = 0; i < ids.length; i++) {
			ODepartment department = new ODepartment();
			department.setDepartId(Integer.valueOf(ids[i]));
			department.setTypeId(typeSet.getId());
			this.typeSetDao.saveDepartment(department);
		}
	}

	public void deleteType(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql = "delete from ODepartment where typeId in(" + id + ")";
			this.typeSetDao.executeHql(hql);
			hql = "delete from OTypeSet where id in(" + id + ")";
			this.typeSetDao.executeHql(hql);
		}
	}

	public String id(String[] ids) {
		String id = "";
		for (int i = 0; i < ids.length; i++) {
			if (id == "")
				id = id + ids[i];
			else {
				id = id + "," + ids[i];
			}
		}
		return id;
	}

	public void deleteWorkFlow(int flowId, int tableId) {
		OWorkflow workflow = getWorkflow(flowId);
		OFormDefinition definition = this.flowFormService.getFormDefinition(workflow.getFormId().intValue());
		OFlowForm form = this.flowFormService.getFlowFormById(definition.getFormId().intValue());
		String instanceId = this.flowFormService.getInstanceId(form.getId().intValue(), tableId);
		String flowSql = "delete from " + form.getTableName() + " where id =" + tableId;
		this.workflowManagerDao.executeSql(flowSql);
		String prossHql = "delete from OProcessinstance where id=" + instanceId;
		this.workflowManagerDao.executeHql(prossHql);
		String hisHql = "delete from OProcessHis where processId=" + instanceId;
		this.workflowManagerDao.executeHql(hisHql);
		String hHql = "delete from OHoliday where instanceId=" + instanceId;
		this.workflowManagerDao.executeHql(hHql);
		String lHql = "delete from OLeave where instanceId=" + instanceId;
		this.workflowManagerDao.executeHql(lHql);
	}

	public List<OProcessHis> getProcessList(int flowId) {
		String hql = "from OProcessHis where processId=" + flowId + " order by id asc";
		return this.workflowManagerDao.findByHql(hql);
	}

	public String getNodeName(Integer id) {
		ONode onode = new ONode();
		if (id == null) {
			onode.setName("开始");
		} else {
			onode = (ONode) this.workflowManagerDao.getObject(new ONode(), id.intValue());
			if (onode == null) {
				onode = new ONode();
				onode.setName("结束");
			}
		}
		return onode.getName();
	}

	public String getUserName(String userId) {
		String name = "";
		List ou = new ArrayList();
		String hql = "from OUser where id in(" + userId + ")";
		ou = this.workflowManagerDao.findByHql(hql);
		for (int i = 0; i < ou.size(); i++) {
			if (name == "")
				name = name + ((OUser) ou.get(i)).getName();
			else {
				name = name + "，" + ((OUser) ou.get(i)).getName();
			}
		}
		return name;
	}

	public void saveWorkFlow(OWorkflow workflow, String orgs, String names, String proportyId, String users, String userNames) {
		this.workflowManagerDao.saveWorkflow(workflow);
		if ((orgs != null) && (!"".equals(orgs))) {
			String[] array = orgs.split(",");
			String[] namesArr = names.split(",");
			for (int i = 0; i < array.length; i++) {
				OAuthority authority = new OAuthority();
				authority.setFlowId(workflow.getId());
				authority.setOrgId(Integer.valueOf(Integer.parseInt(array[i])));
				authority.setOrgName(namesArr[i]);
				authority.setType("org");
				this.workflowManagerDao.saveAuthority(authority);
			}
		}
		if ((users != null) && (!"".equals(users))) {
			String[] array = users.split(",");
			String[] namesArr = userNames.split(",");
			for (int i = 0; i < array.length; i++) {
				OAuthority authority = new OAuthority();
				authority.setFlowId(workflow.getId());
				authority.setOrgId(Integer.valueOf(Integer.parseInt(array[i])));
				authority.setOrgName(namesArr[i]);
				authority.setType("user");
				this.workflowManagerDao.saveAuthority(authority);
			}
		}
		if ((proportyId != null) && (!"".equals(proportyId))) {
			String[] ids = proportyId.split(",");
			for (int i = 0; i < ids.length; i++) {
				OFormColumn column = new OFormColumn();
				column.setFlowId(workflow.getId());
				column.setProportyId(Integer.valueOf(Integer.parseInt(ids[i])));
				this.workflowManagerDao.saveFormColumn(column);
			}
		}
	}

	private void sendMessage(String users, String title) {
		if ((users != null) && (!"".equals(users))) {
			String[] userArr = users.split(",");
			for (int i = 0; i < userArr.length; i++)
				if ((userArr[i] != null) && (!"".equals(userArr[i]))) {
					OUser user = (OUser) this.workflowManagerDao.getObject(new OUser(), Integer.parseInt(userArr[i]));
					if ((user.getPhone() != null) && (!"".equals(user.getPhone())))
						this.messageService.send(user.getPhone(), "您有新的" + title + "文件,请尽快处理!");
				}
		}
	}

	public void updateWorkFlow(OWorkflow workflow, String orgs, String names, String proportyId, String users, String userNames) {
		String hql = "delete from OAuthority where flowId=" + workflow.getId();
		this.workflowManagerDao.executeHql(hql);
		this.workflowManagerDao.updateWorkflow(workflow);
		if ((orgs != null) && (!"".equals(orgs))) {
			String[] array = orgs.split(",");
			String[] namesArr = names.split(",");
			for (int i = 0; i < array.length; i++) {
				OAuthority authority = new OAuthority();
				authority.setFlowId(workflow.getId());
				authority.setOrgId(Integer.valueOf(Integer.parseInt(array[i])));
				authority.setOrgName(namesArr[i]);
				authority.setType("org");
				this.workflowManagerDao.saveAuthority(authority);
			}
		}
		if ((users != null) && (!"".equals(users))) {
			String[] array = users.split(",");
			String[] namesArr = userNames.split(",");
			for (int i = 0; i < array.length; i++) {
				OAuthority authority = new OAuthority();
				authority.setFlowId(workflow.getId());
				authority.setOrgId(Integer.valueOf(Integer.parseInt(array[i])));
				authority.setOrgName(namesArr[i]);
				authority.setType("user");
				this.workflowManagerDao.saveAuthority(authority);
			}
		}
		String hql1 = "delete from OFormColumn where flowId=" + workflow.getId();
		this.workflowManagerDao.executeHql(hql1);
		if ((proportyId != null) && (!"".equals(proportyId))) {
			String[] ids = proportyId.split(",");
			for (int i = 0; i < ids.length; i++) {
				OFormColumn column = new OFormColumn();
				column.setFlowId(workflow.getId());
				column.setProportyId(Integer.valueOf(Integer.parseInt(ids[i])));
				this.workflowManagerDao.saveFormColumn(column);
			}
		}
	}

	public void deleteWorkflow(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql = "delete from OWorkflow f where f.id in(" + id + ")";
			this.workflowManagerDao.executeHql(hql);
		}
	}

	public List<OWorkflow> getWorkFlowList(String typeId, String name, int page) {
		String hql = "from OWorkflow o where o.typeId is not null";
		if ((typeId != null) && (!"".equals(typeId))) {
			hql = hql + " and typeId=" + typeId;
		}
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and  name like '%" + name.trim() + "%'";
		}
		hql = hql + "  order by o.typeId,o.createTime desc";
		return this.workflowManagerDao.findByPage(hql, page);
	}

	public List<OWorkflow> getReceiveWorkFlowList(int page) {
		String hql = "from OWorkflow o order by o.id";
		return this.workflowManagerDao.findByPage(hql, page);
	}

	public List<OWorkflow> getWorkFlowListByStatus() {
		String hql1 = "from ONode o where o.status=0";
		List list = this.workflowManagerDao.findByHql(hql1);
		if (list != null) {
			String ids = "";
			for (int i = 0; i < list.size(); i++) {
				if (ids == "")
					ids = ids + ((ONode) list.get(i)).getFlowId();
				else {
					ids = ids + "," + ((ONode) list.get(i)).getFlowId();
				}
			}
			if (ids == "")
				return null;
			String hql = "from OWorkflow o where o.id in(" + ids + ")" + " order by o.createTime desc";
			return this.workflowManagerDao.findByHql(hql);
		}

		return null;
	}

	public List<OWorkflow> getOrgWorkFlow(OUser orgId) {
		String hql = "from OWorkflow o where o.id in (select flowId from OAuthority where orgId in(" + orgId.getId() + ","
				+ strLis(getOrgAndParent(orgId.getOrgId().intValue())) + ")) and o.typeId is not null";
		return this.workflowManagerDao.findByHql(hql);
	}

	public Long countOrgWorkFlow(int orgId) {
		String hql = "select count(o.id) from OWorkflow o where o.id in (select flowId from OAuthority where orgId in(" + strLis(getOrgAndParent(orgId))
				+ ")) and o.typeId is not null";
		return this.workflowManagerDao.countTotalByHql(hql);
	}

	public List<OWorkflow> getReceiveFlowList(OUser orgId) {
		String hql = " from OWorkflow where id in( select flowId from OAuthority o where o.orgId in(" + orgId.getId() + ","
				+ strLis(getOrgAndParent(orgId.getOrgId().intValue())) + ")) and formId=2 and  typeId is null";
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<OWorkflow> getWorkFlowManager(int formId, int currentPage) {
		String hql = "from OWorkflow w where w.typeId is null and w.formId=" + formId;
		return this.workflowManagerDao.findByPage(hql, currentPage);
	}

	public Long countWorkFlow(int formId) {
		String hql = "select count(w.id) from OWorkflow w where w.typeId is null and  w.formId=" + formId;
		return this.workflowManagerDao.countTotalByHql(hql);
	}

	public List<OWorkflow> getDispatcherFlowList(OUser orgId) {
		String hql = " from OWorkflow where id in( select flowId from OAuthority o where o.orgId in(" + orgId.getId() + ","
				+ strLis(getOrgAndParent(orgId.getOrgId().intValue())) + ")) and formId=1";
		return this.workflowManagerDao.findByHql(hql);
	}

	public String strLis(List list) {
		String id = "";
		int i = 0;
		for (int a = list.size(); i < a; i++) {
			if (id == "")
				id = id + list.get(i);
			else {
				id = id + "," + list.get(i);
			}
		}
		return id;
	}

	public OWorkflow getWorkflow(int id) {
		return (OWorkflow) this.workflowManagerDao.getObject(new OWorkflow(), id);
	}

	public List<OAuthority> getAuthorityFlow(int flowId) {
		String hql = "from OAuthority o where o.flowId=" + flowId;
		return this.workflowManagerDao.findByHql(hql);
	}

	public long countWorkFlow(String typeId, String name) {
		String hql = "select count(id) from OWorkflow where  typeId is not null";
		if ((typeId != null) && (!"".equals(typeId))) {
			hql = hql + " and typeId=" + typeId;
		}
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and  name like '%" + name.trim() + "%'";
		}
		return this.workflowManagerDao.countTotalByHql(hql).longValue();
	}

	public List<OWorkflow> workFlowTypeList() {
		String hql = " from OWorkflow where  typeId is not null";
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<OWorkflow> workFlowAllList() {
		String hql = " from OWorkflow ";
		return this.workflowManagerDao.findByHql(hql);
	}

	public long countReceiveWorkFlow() {
		String hql = "select count(*) from OWorkflow where formId=1";
		return this.workflowManagerDao.countTotalByHql(hql).longValue();
	}

	public List<ONode> getBackNodeList(int instanceId, int nodeId) {
		String hql = "from OProcessHis  where processId=" + instanceId + " and id <(select max(h.id) from OProcessHis h where h.nodeId=" + nodeId
				+ " and processId=" + instanceId + ") order by id asc";
		List<OProcessHis> list = this.workflowManagerDao.findByHql(hql);
		List nodeList = new ArrayList();
		LinkedHashMap map = new LinkedHashMap();
		for (OProcessHis his : list) {
			ONode node = getFlowNode(his.getNodeId().intValue());
			if (map.get(his.getNodeId()) == null) {
				map.put(his.getNodeId(), node);
			}
		}
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			ONode node = (ONode) map.get(it.next());
			nodeList.add(node);
		}
		return nodeList;
	}

	public void saveFlowType(OFlowType flowType) {
		this.workflowManagerDao.saveFlowType(flowType);
	}

	public OFlowType getFlowType(int id) {
		return (OFlowType) this.workflowManagerDao.getObject(new OFlowType(), id);
	}

	public void updateFlowType(OFlowType flowType) {
		this.workflowManagerDao.updateFlowType(flowType);
	}

	public List<OFlowType> getFlowTypeList() {
		String hql = "from OFlowType t order by t.createTime desc";
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<OFlowType> getFlowTypeList(int page) {
		String hql = "from OFlowType t order by t.id";
		return this.workflowManagerDao.findByPage(hql, page);
	}

	public long countFlowType() {
		String hql = "select count(*) from OFlowType";
		return this.workflowManagerDao.countTotalByHql(hql).longValue();
	}

	public void deleteFlowType(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql1 = "delete from OFlowType f where f.id in(" + id + ")";
			String hql2 = "delete from OWorkflow f where f.typeId in(" + id + ")";
			this.workflowManagerDao.executeHql(hql1);
			this.workflowManagerDao.executeHql(hql2);
		}
	}

	public synchronized void updateRejectWorkFlow(int ids, int flowId, String nodeId, OUser user, String message) {
		OWorkflow workflow = getWorkflow(flowId);
		OFormDefinition definition = this.flowFormService.getFormDefinition(workflow.getFormId().intValue());
		String instanceId = this.flowFormService.getInstanceId(definition.getFormId().intValue(), ids);
		OProcessinstance instance = getProcessinstance(Integer.parseInt(instanceId));
		String title = workflow.getName() + "的公文被退回";
		String content = workflow.getName() + "的公文被" + user.getName() + "退回";
		if ((nodeId == null) || ("".equals(nodeId))) {
			String hql = "delete from OProcessHis where processId=" + instanceId;
			this.workflowManagerDao.executeHql(hql);
			String hql1 = "delete from OProcessinstance where id=" + instanceId;
			this.workflowManagerDao.executeHql(hql1);
			this.flowFormService.updateRejectDraft(definition.getFormId().intValue(), ids, user);
			String actor = this.flowFormService.getFlowCreatUser(definition.getFormId().intValue(), ids);
			OUser u = (OUser) this.formDao.getObject(new OUser(), Integer.parseInt(actor));
			this.mailService.saveSendInMail(title, content, actor, u.getName(), user);
		} else {
			String hql = "from OProcessHis where startNode=" + nodeId + " and processId=" + instanceId;
			List list = this.workflowManagerDao.findByHql(hql);
			if ((list != null) && (!list.isEmpty())) {
				OProcessHis his = (OProcessHis) list.get(0);
				this.mailService.saveSendInMail(title, content, his.getExecutorId().toString(), his.getExecutor(), user);
				createProssinstance(instanceId, flowId, Integer.parseInt(nodeId), his.getExecutorId().toString(), user.getId().intValue(), message,
						workflow.getName());
				this.flowFormService.updateRejectNode(definition.getFormId().intValue(), ids, user);
				saveProssHis(instance.getId().intValue(), Integer.parseInt(nodeId), user, his.getExecutorId().toString(), "退回");
			}
		}
	}

	public String flowListHtml(List<OWorkflow> workList, String userId) {
		StringBuffer tabeHtml = new StringBuffer();
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		int c = 1;
		int s = 0;
		for (int i = 0; i < workList.size(); i++) {
			tabeHtml.append("<tr id='row' onmouseover='overStyle(this)' onmouseout='outStyle(this)'>");
			tabeHtml.append("<td align='center'>");
			if (hasPermission("" + ((OWorkflow) workList.get(i)).getUserId(), userId)) {
				tabeHtml.append("<input type='checkbox' name='ids' value='" + ((OWorkflow) workList.get(i)).getId() + "' onclick='checkodd()' />");
			}
			tabeHtml.append("</td>");
			for (int j = i + 1; j < workList.size(); j++) {
				if (((OWorkflow) workList.get(i)).getTypeId().equals(((OWorkflow) workList.get(j)).getTypeId())) {
					c++;
				}
			}
			if (((OWorkflow) workList.get(i)).getTypeId().equals(Integer.valueOf(s))) {
				c = 1;
			}
			if (c > 1) {
				tabeHtml.append("<td rowspan='" + c + "'>" + ((OWorkflow) workList.get(i)).getTypeName() + "</td>");
				s = ((OWorkflow) workList.get(i)).getTypeId().intValue();
				c = 1;
			} else if (!((OWorkflow) workList.get(i)).getTypeId().equals(Integer.valueOf(s))) {
				tabeHtml.append("<td>" + ((OWorkflow) workList.get(i)).getTypeName() + "</td>");
			}
			tabeHtml.append("<td>" + ((OWorkflow) workList.get(i)).getName() + "</td>");
			tabeHtml.append("<td>" + ((OWorkflow) workList.get(i)).getNote() + "</td>");
			tabeHtml.append("<td>" + ((OWorkflow) workList.get(i)).getUserName() + "</td>");
			tabeHtml.append("<td>" + bartDateFormat.format(((OWorkflow) workList.get(i)).getCreateTime()) + "</td>");
			tabeHtml.append("<td align='center'>");
			if (hasPermission("" + ((OWorkflow) workList.get(i)).getUserId(), userId)) {
				tabeHtml.append("<img alt='修改' onclick='workflowupdate(" + ((OWorkflow) workList.get(i)).getId()
						+ ")' src='image/operate_update.gif' style='cursor: pointer'>&nbsp;&nbsp;<a href='flowNodeManager.action?flowId="
						+ ((OWorkflow) workList.get(i)).getId() + "'><img alt='设置' src='image/standardpart.gif' border='0'/></a>");
			}
			tabeHtml.append("</td></tr>");
		}
		return tabeHtml.toString();
	}

	public boolean hasPermission(String userId, String sessionUser) {
		boolean flag = false;
		boolean permission = false;
		if ((userId != null) && (!"".equals(userId))) {
			String curenthql = "from ORole where id in (select roleId from ORoleUser where userId =" + sessionUser + ")";
			List<ORole> sessionRoles = this.workflowManagerDao.findByHql(curenthql);
			for (ORole role : sessionRoles) {
				if ("4".equals(role.getType())) {
					permission = true;
					return permission;
				}
			}
			if ((!permission) && (sessionUser.equals(userId)))
				flag = true;
		} else {
			flag = true;
		}
		return flag;
	}

	public List<ONode> findNodeListByFlow(int flowId) {
		String hql = "from ONode o where o.flowId=" + flowId + "order by o.status";
		return this.workflowManagerDao.findByHql(hql);
	}

	public void saveFlowNode(ONode node, String proportyId, String users) {
		this.workflowManagerDao.saveFlowNode(node);
		saveFormNode(node.getId().intValue(), proportyId);
		if ("4".equals(node.getActorType()))
			saveTaskActors(null, node.getId().intValue());
		else
			saveTaskActors(users, node.getId().intValue());
	}

	public void deleteFlowNode(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql = "delete from ONode f where f.id in(" + id + ")";
			this.workflowManagerDao.executeHql(hql);
		}
	}

	public ONode getFlowNode(int id) {
		return (ONode) this.workflowManagerDao.getObject(new ONode(), id);
	}

	public ONode getStartNode(int flowId) {
		String hql = "from ONode o where o.flowId=" + flowId + "and status=0";
		return this.workflowManagerDao.getStartNode(hql);
	}

	public List<ONode> getStartNodeList(List<OWorkflow> list) {
		String hql = "from ONode o where status=0";
		String ids = "";
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (ids == "")
					ids = ids + ((OWorkflow) list.get(i)).getId();
				else {
					ids = ids + ((OWorkflow) list.get(i)).getId();
				}
			}
			hql = hql + "and o.flowId in(" + ids + ")";
			return this.workflowManagerDao.findByHql(hql);
		}
		return null;
	}

	public void updateFlowNode(ONode node) {
		String hql = "delete from OTaskActor where nodeId=" + node.getId();
		this.workflowManagerDao.executeHql(hql);
		this.workflowManagerDao.updateFlowNode(node);
	}

	public void updateFlowNodeForm(ONode node, String proportyId, String users) {
		String hql = "delete from OTaskActor where nodeId=" + node.getId();
		this.workflowManagerDao.executeHql(hql);
		this.workflowManagerDao.updateFlowNode(node);
		updateFormNode(node.getId().intValue(), proportyId);
		if ("4".equals(node.getActorType()))
			saveTaskActors(null, node.getId().intValue());
		else
			saveTaskActors(users, node.getId().intValue());
	}

	public boolean hasActor(String[] users, int index) {
		return users[index] != null;
	}

	public List<ONode> getNodeListByUser(int userid) {
		String hql = "from ONode o where o.userId=" + userid;
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<OTaskActor> getTaskActorList(int nodeId) {
		String hql = "from OTaskActor o where o.nodeId=" + nodeId;
		return this.workflowManagerDao.findByHql(hql);
	}

	public void saveTaskActor(OTaskActor taskActor) {
		this.workflowManagerDao.saveTaskActor(taskActor);
	}

	public void saveTaskActors(String users, int nodeId) {
		if ((users != null) && (!"".equals(users))) {
			String[] ids = users.split(",");
			for (int i = 0; i < ids.length; i++) {
				OTaskActor taskActor = new OTaskActor();
				taskActor.setActor(Integer.valueOf(Integer.parseInt(ids[i])));
				taskActor.setNodeId(Integer.valueOf(nodeId));
				this.workflowManagerDao.saveTaskActor(taskActor);
			}
		}
	}

	public void deleteTaskActor(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql = "delete from ONode f where f.id in(" + id + ")";
			this.workflowManagerDao.executeHql(hql);
		}
	}

	public void deleteTaskActorByNodeId(String[] ids) {
		if (ids != null) {
			String id = id(ids);
			String hql = "delete from OTaskActor f where f.nodeId in(" + id + ")";
			this.workflowManagerDao.executeHql(hql);
		}
	}

	public void updateTaskActor(OTaskActor taskActor) {
		this.workflowManagerDao.updateTaskActor(taskActor);
	}

	public String[] serachActorNames(List<OTaskActor> list, String type) {
		String[] values = new String[2];
		values[0] = "";
		values[1] = "";
		if (type.equals("3")) {
			for (OTaskActor ta : list) {
				OOrg org = (OOrg) this.workflowManagerDao.getObject(new OOrg(), ta.getActor().intValue());
				if (org != null)
					if (values[0] == "") {
						int tmp92_91 = 0;
						String[] tmp92_90 = values;
						tmp92_90[tmp92_91] = (tmp92_90[tmp92_91] + org.getId());
						int tmp119_118 = 1;
						String[] tmp119_117 = values;
						tmp119_117[tmp119_118] = (tmp119_117[tmp119_118] + org.getName());
					} else {
						int tmp149_148 = 0;
						String[] tmp149_147 = values;
						tmp149_147[tmp149_148] = (tmp149_147[tmp149_148] + "," + org.getId());
						int tmp181_180 = 1;
						String[] tmp181_179 = values;
						tmp181_179[tmp181_180] = (tmp181_179[tmp181_180] + "," + org.getName());
					}
			}
		} else {
			for (OTaskActor ta : list) {
				OUser user = (OUser) this.workflowManagerDao.getObject(new OUser(), ta.getActor().intValue());
				if (user != null) {
					if (values[0] == "") {
						int tmp291_290 = 0;
						String[] tmp291_289 = values;
						tmp291_289[tmp291_290] = (tmp291_289[tmp291_290] + user.getId());
						int tmp318_317 = 1;
						String[] tmp318_316 = values;
						tmp318_316[tmp318_317] = (tmp318_316[tmp318_317] + user.getName());
					} else {
						int tmp348_347 = 0;
						String[] tmp348_346 = values;
						tmp348_346[tmp348_347] = (tmp348_346[tmp348_347] + "," + user.getId());
						int tmp380_379 = 1;
						String[] tmp380_378 = values;
						tmp380_378[tmp380_379] = (tmp380_378[tmp380_379] + "," + user.getName());
					}
				}
			}
		}
		return values;
	}

	public List<OTaskActor> getExecutorList(int nodeid) {
		String hql = "from OTaskActor o where o.nodeId=" + nodeid;
		return this.workflowManagerDao.findByHql(hql);
	}

	public String getAllCandidate(List<OTaskActor> list) {
		String ids = "";
		StringBuffer buff = new StringBuffer();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (ids == "")
					ids = ids + ((OTaskActor) list.get(i)).getActor();
				else {
					ids = ids + "," + ((OTaskActor) list.get(i)).getActor();
				}
			}
		}
		String hql = "from OUser o where o.id in (" + ids + ")";
		List users = this.workflowManagerDao.findByHql(hql);
		buff.append("[");
		for (int i = 0; i < users.size(); i++) {
			OUser user = (OUser) users.get(i);
			buff.append("{");
			buff.append("\"id\": \"" + user.getId() + "\", \"text\": \"" + user.getName() + "\", \"iconCls\": \"icon-ok\"");
			buff.append("}");
			if (i != users.size() - 1) {
				buff.append(",");
			}
		}
		buff.append("]");
		return buff.toString();
	}

	public String getAllOrg(List<OTaskActor> list) {
		String ids = "";
		StringBuffer buff = new StringBuffer();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (ids == "")
					ids = ids + ((OTaskActor) list.get(i)).getActor();
				else {
					ids = ids + "," + ((OTaskActor) list.get(i)).getActor();
				}
			}
		}
		String hql = "from OOrg o where o.id in (" + ids + ")";
		List orgList = this.workflowManagerDao.findByHql(hql);
		buff.append("[");
		for (int i = 0; i < orgList.size(); i++) {
			OOrg org = (OOrg) orgList.get(i);
			buff.append("{");
			buff.append("\"id\": \"" + org.getId() + "\", \"text\": \"" + org.getName() + "\", \"iconCls\": \"icon-ok\", \"state\": \"closed\"");
			buff.append("}");
			if (i != orgList.size() - 1) {
				buff.append(",");
			}
		}
		buff.append("]");
		return buff.toString();
	}

	public List<OUser> getUserList(List<OTaskActor> list) {
		String ids = "";
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (ids == "")
					ids = ids + ((OTaskActor) list.get(i)).getActor();
				else {
					ids = ids + "," + ((OTaskActor) list.get(i)).getActor();
				}
			}
		}
		String hql = "from OUser o where o.id in (" + ids + ")";
		return this.workflowManagerDao.findByHql(hql);
	}

	public String getTaskActorAsArray(int nodeid) {
		String hql = "from OTaskActor o where o.nodeId=" + nodeid;
		List<OTaskActor> list = this.workflowManagerDao.findByHql(hql);
		String actors = "";
		for (OTaskActor ta : list) {
			if (actors == "")
				actors = actors + ta.getActor();
			else {
				actors = actors + "," + ta.getActor();
			}
		}
		return actors;
	}

	public List<OOrg> getOrgList(String ids) {
		String hql = "from OOrg o where o.id in (" + ids + ")";
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<ONode> findBackNodes(int nodeid, int flowid, String status) {
		if (status.equals("2")) {
			return null;
		}
		String hql1 = "from ONode o where o.flowId=" + flowid + "and o.id<>" + nodeid + "and o.status<>0";
		List<ONode> nodes = this.workflowManagerDao.findByHql(hql1);
		Map<Integer, ONode> nodemap = new HashMap<Integer, ONode>();
		for (ONode o : nodes) {
			nodemap.put(o.getId(), o);
		}

		String hql2 = "from ONodeTransaction o where o.startId=" + nodeid + "and o.flowId=" + flowid;
		List<ONodeTransaction> transaction = this.workflowManagerDao.findByHql(hql2);
		for (ONodeTransaction nt : transaction) {
			nodemap.remove(nt.getEndId());
		}
		String hql3 = "from ONodeTransaction o where o.endId=" + nodeid + "and o.flowId=" + flowid;
		transaction = this.workflowManagerDao.findByHql(hql3);
		for (ONodeTransaction nt : transaction) {
			nodemap.remove(nt.getStartId());
		}
		Object backnodes = new ArrayList();
		for (ONode node : nodemap.values()) {
			((List) backnodes).add(node);
		}
		return (List<ONode>) backnodes;
	}

	public List<ONode> findAllNodes(int flowid) {
		String hql1 = "from ONode o where o.flowId=" + flowid;
		List nodes = this.workflowManagerDao.findByHql(hql1);
		return nodes;
	}

	public List<ONode> findChosenBack(int nodeid, int flowid, String status) {
		String hql = "from ONodeTransaction o where o.startId=" + nodeid + "and o.flowId=" + flowid;
		List<ONodeTransaction> transaction = this.workflowManagerDao.findByHql(hql);
		List frontnodes = new ArrayList();
		for (ONodeTransaction o : transaction) {
			ONode node = null;
			if (o.getEndId().intValue() == 0) {
				node = new ONode();
				node.setId(Integer.valueOf(0));
				node.setName("结束");
			} else {
				node = (ONode) this.workflowManagerDao.getObject(new ONode(), o.getEndId().intValue());
			}
			frontnodes.add(node);
		}
		return frontnodes;
	}

	public List<ONode> findFrontNodes(int nodeid, int flowid, String status) {
		if (status.equals("0")) {
			return null;
		}
		String hql = "from ONodeTransaction o where o.endId=" + nodeid + "and o.flowId=" + flowid;
		List<ONodeTransaction> transaction = this.workflowManagerDao.findByHql(hql);
		List<ONode> frontnodes = new ArrayList<ONode>();
		for (ONodeTransaction o : transaction) {
			ONode node = null;
			if (o.getStartId().intValue() == 0) {
				node = new ONode();
				node.setId(Integer.valueOf(0));
				node.setName("结束");
			} else {
				node = (ONode) this.workflowManagerDao.getObject(new ONode(), o.getStartId().intValue());
			}
			frontnodes.add(node);
		}
		return frontnodes;
	}

	public void saveNodeTransitionsSet(int start, String[] ends, String flowid) {
		String hql = "delete from ONodeTransaction where startId=" + start;
		this.workflowManagerDao.executeHql(hql);
		ONode s = (ONode) this.workflowManagerDao.getObject(new ONode(), start);
		for (int i = 0; i < ends.length; i++) {
			int index = Integer.parseInt(ends[i]);
			ONodeTransaction nodeTransaction = new ONodeTransaction();
			if (index != 0) {
				ONode node = (ONode) this.workflowManagerDao.getObject(new ONode(), index);
				nodeTransaction.setStartId(s.getId());
				nodeTransaction.setStartName(s.getName());
				nodeTransaction.setEndId(node.getId());
				nodeTransaction.setEndName(node.getName());
				nodeTransaction.setFlowId(flowid);
			} else {
				nodeTransaction.setStartId(s.getId());
				nodeTransaction.setStartName(s.getName());
				nodeTransaction.setEndId(Integer.valueOf(0));
				nodeTransaction.setEndName("结束");
				nodeTransaction.setFlowId(flowid);
			}
			this.workflowManagerDao.saveNodeTransition(nodeTransaction);
		}
	}

	public void deleteNodeTransitionByNodeId(String[] ids) {
		for (int i = 0; i < ids.length; i++) {
			String hql = "delete from ONodeTransaction o where o.startId=" + ids[i] + "or o.endId=" + ids[i];
			this.workflowManagerDao.executeHql(hql);
		}
	}

	public boolean hasStartOrEndNode(String status, int flowid) {
		if (status.equals("1")) {
			return false;
		}
		String hql = "select count(*) from ONode o where o.flowId=" + flowid + "and o.status=" + status;
		long count = this.workflowManagerDao.countTotalByHql(hql).longValue();

		return count != 0L;
	}

	public boolean hasStartOrEndNode(String status, int flowid, Integer nodeid) {
		boolean flag = hasStartOrEndNode(status, flowid);
		if (!flag) {
			return false;
		}
		String hql = "select count(*) from ONode o where o.flowId=" + flowid + "and o.status =" + status + " and o.id=" + nodeid;
		long count = this.workflowManagerDao.countTotalByHql(hql).longValue();

		return count == 0L;
	}

	public List<ONode> getNextNode(int nodeid) {
		List<ONode> nodes = new ArrayList<ONode>();
		String hql = "from ONodeTransaction o where o.startId=" + nodeid;
		List<ONodeTransaction> list = this.workflowManagerDao.findByHql(hql);
		for (ONodeTransaction nt : list) {
			ONode node = (ONode) this.workflowManagerDao.getObject(new ONode(), nt.getEndId().intValue());
			if (node != null) {
				nodes.add(node);
			} else {
				node = new ONode();
				node.setId(Integer.valueOf(0));
				node.setName("结束");
				nodes.add(node);
			}
		}
		return nodes;
	}

	public ONodeTransaction getONodeTransaction(int id) {
		return (ONodeTransaction) this.workflowManagerDao.getObject(new ONodeTransaction(), id);
	}

	public List<ONodeTransaction> getNodeTransactionList(int flowid) {
		String hql = "from ONodeTransaction o where o.flowId=" + flowid;
		return this.workflowManagerDao.findByHql(hql);
	}

	public List<ONodeTransaction> getNodeTransactionList(int flowid, int nodeid) {
		String hql = "from ONodeTransaction o where o.flowId=" + flowid + "and o.startId=" + nodeid;
		return this.workflowManagerDao.findByHql(hql);
	}

	public void saveNodeTransition(ONodeTransaction nodeTransaction) {
		this.workflowManagerDao.saveNodeTransition(nodeTransaction);
	}

	public void deleteNodeTransition(ONodeTransaction nodeTransaction) {
		this.workflowManagerDao.deleteNodeTransition(nodeTransaction);
	}

	public void saveProcessinstance(OProcessinstance processinstance) {
		this.workflowManagerDao.saveOProcessinstance(processinstance);
	}

	public String gethis(OUser userId) {
		String hql = " select o.id from OProcessinstance o,OProcessHis t where o.id = t.processId and o.nodeStatus ='task' and t.executorId=" + userId.getId();
		return hql;
	}

	public String getSqlhis(OUser userId) {
		String sql = " select o.id from o_processinstance o,o_process_his t where o.id = t.process_id and o.node_status ='task' and t.executor_id="
				+ userId.getId();
		return sql;
	}

	public String getProcessinstanceList(int userId, List<Integer> agent) {
		String hql = "select t.id from OProcessinstance t where ','|| t.executor ||','  LIKE   '%,'|| " + userId + "||',%'";
		if ((agent != null) && (agent.size() > 0)) {
			for (int i = 0; i < agent.size(); i++) {
				hql = hql + "  or ','|| t.executor ||','  LIKE   '%,'|| " + agent.get(i) + "||',%' ";
			}
		}
		return hql;
	}

	public String getSqlProcessinstanceList(int userId, List<Integer> agent) {
		String sql = "select t.id from o_processinstance t where ','|| t.executor ||','  LIKE   '%,'|| " + userId + "||',%'";
		if ((agent != null) && (agent.size() > 0)) {
			for (int i = 0; i < agent.size(); i++) {
				sql = sql + "  or ','|| t.executor ||','  LIKE   '%,'|| " + agent.get(i) + "||',%' ";
			}
		}
		return sql;
	}

	public String getAdminProcessList(String task) {
		String hql = "select id from OProcessinstance o where o.nodeStatus ='" + task + "'";
		return hql;
	}

	public String getSqlAdminProcessList(String task) {
		String hql = "select id from o_processinstance o where o.node_status ='" + task + "'";
		return hql;
	}

	public String getDoneProcessinstanceList(int userid) {
		String hql = " select o.id from OProcessinstance o,OProcessHis t where  o.id =t.processId and o.nodeStatus ='end' and  t.executorId =" + userid;
		return hql;
	}

	public String getSqlDoneProcessinstanceList(int userid) {
		String sql = " select o.id from o_processinstance o,o_process_his t where  o.id =t.process_id and o.node_status ='end' and  t.executor_id =" + userid;
		return sql;
	}

	public OProcessinstance getProcessinstance(int id) {
		return (OProcessinstance) this.workflowManagerDao.getObject(new OProcessinstance(), id);
	}

	public void deleteProcessHis(OProcessHis processHistory) {
		this.workflowManagerDao.deleteProcessHis(processHistory);
	}

	public void saveProcessHis(OProcessHis processHistory) {
		this.workflowManagerDao.saveProcessHis(processHistory);
	}

	public void updateProcessHis(OProcessHis processHistory) {
		this.workflowManagerDao.updateProcessHis(processHistory);
	}

	public List<OProcessHis> getProcessHistoryByProcessId(int id) {
		String hql = "from OProcessHis o where o.processId=" + id;
		return this.workflowManagerDao.findByHql(hql);
	}

	public ONode getPreviousNode(List<OProcessHis> list, int nodeId) {
		String hql = "from OProcessHis o where o.endNode= " + nodeId;
		List his = this.workflowManagerDao.findByHql(hql);
		if (his.size() == 0) {
			return null;
		}
		int id = ((OProcessHis) his.get(0)).getStartNode().intValue();
		return (ONode) this.workflowManagerDao.getObject(new ONode(), id);
	}

	public OProcessHis getProcessHistoryByNode(int end, List<OProcessHis> list) {
		for (OProcessHis processHis : list) {
			if (processHis.getEndNode().equals(Integer.valueOf(end))) {
				return processHis;
			}
		}
		return null;
	}

	public OProcessHis getProcessHistory(int processId, int startNode) {
		String hql = "from OProcessHis o where o.processId=" + processId + "and o.startNode=" + startNode;
		List list = this.workflowManagerDao.findByHql(hql);
		if (list.size() != 0) {
			return (OProcessHis) list.get(0);
		}
		return null;
	}

	public boolean nextIsRejectDoc(int nodeId, int processId) {
		String hql = "from OProcessHis o where o.startNode=" + nodeId + " and o.processId=" + processId;
		List list = this.workflowManagerDao.findByHql(hql);

		return (list != null) && (list.size() != 0);
	}

	public List<OFormType> getFormTypeList() {
		String hql = "from OFormType";
		return this.workflowManagerDao.findByHql(hql);
	}

	public OFormType getFormType(int id) {
		return (OFormType) this.formDao.getObject(new OFormType(), id);
	}

	public List<OForm> getFormList(int formTypeId) {
		String hql = "from OForm o where o.formTypeId=" + formTypeId;
		return this.formDao.findByHql(hql);
	}

	public OForm getForm(int id) {
		return (OForm) this.formDao.getObject(new OForm(), id);
	}

	public List<OFormProporty> getFormProportyList(int formId) {
		String hql = "from OFormProporty o where o.formId=" + formId;
		return this.formDao.findByHql(hql);
	}

	public void saveFormNode(int nodeId, String propertyid) {
		if ((propertyid != null) && (!"".equals(propertyid))) {
			String[] ids = propertyid.split(",");
			for (int i = 0; i < ids.length; i++) {
				OFormNode formNode = new OFormNode();
				formNode.setNodeId(Integer.valueOf(nodeId));
				formNode.setProportyId(Integer.valueOf(Integer.parseInt(ids[i])));
				formNode.setDisAble(Integer.valueOf(1));
				formNode.setWriteAble(Integer.valueOf(0));
				formNode.setMustBe(Integer.valueOf(0));
				this.formDao.saveForm(formNode);
			}
		}
	}

	public void updateFormNode(int nodeId, String propertyid) {
		String hql = "delete from OFormNode o where o.nodeId=" + nodeId;
		this.formDao.executeHql(hql);
		if ((propertyid != null) && (!"".equals(propertyid))) {
			String[] ids = propertyid.split(",");
			for (int i = 0; i < ids.length; i++) {
				OFormNode formNode = new OFormNode();
				formNode.setNodeId(Integer.valueOf(nodeId));
				formNode.setProportyId(Integer.valueOf(Integer.parseInt(ids[i])));
				formNode.setDisAble(Integer.valueOf(1));
				formNode.setWriteAble(Integer.valueOf(0));
				formNode.setMustBe(Integer.valueOf(0));
				this.formDao.saveForm(formNode);
			}
		}
	}

	public List<OFormProporty> getChosenFormPropertyList(int nodeId) {
		String hql = "from OFormProporty o where o.id in (select proportyId from OFormNode where nodeId=" + nodeId + ")";
		return this.formDao.findByHql(hql);
	}

	public List<OFormProporty> getChosenFormColumnList(int flowId) {
		String hql = "from OFormProporty o where o.id in (select proportyId from OFormColumn where flowId=" + flowId + ")";
		return this.formDao.findByHql(hql);
	}

	public List<OFlowFormProporty> getChosenPropertyList(int nodeId) {
		String hql = "from OFlowFormProporty o where o.id in(select proportyId from OFormNode where nodeId=" + nodeId + ")";
		return this.formDao.findByHql(hql);
	}

	public List<OFlowFormProporty> getChosenFlowPropertyList(int flowId) {
		String hql = "from OFlowFormProporty o where o.id in(select proportyId from OFormColumn where flowId=" + flowId + ")";
		return this.formDao.findByHql(hql);
	}

	public List<OFormProporty> getUnChosenFormPropertyList(int formId, List<OFormProporty> chosen) {
		String hql = "from OFormProporty o where o.formId=" + formId;
		List<OFormProporty> list = this.formDao.findByHql(hql);
		Map<Integer, OFormProporty> map = new HashMap<Integer, OFormProporty>();
		for (OFormProporty fp : list) {
			map.put(fp.getId(), fp);
		}
		for (OFormProporty fp : chosen) {
			map.remove(fp.getId());
		}
		List<OFormProporty> fplist = new ArrayList<OFormProporty>();
		for (Integer id : map.keySet()) {
			fplist.add((OFormProporty) map.get(id));
		}
		return fplist;
	}

	public List<OFlowFormProporty> getUnChosenPropertyList(int formId, List<OFlowFormProporty> chosen) {
		String hql = "from OFlowFormProporty o where o.formid=" + formId;
		List<OFlowFormProporty> list = this.formDao.findByHql(hql);
		Map<Integer, OFlowFormProporty> map = new HashMap<Integer, OFlowFormProporty>();
		for (OFlowFormProporty fp : list) {
			map.put(fp.getId(), fp);
		}
		for (OFlowFormProporty fp : chosen) {
			map.remove(fp.getId());
		}
		List fplist = new ArrayList();
		for (Integer id : map.keySet()) {
			fplist.add((OFlowFormProporty) map.get(id));
		}
		return fplist;
	}

	public List<OFormProporty> getFormProportyByNode(int nodeId) {
		String hql1 = "from OFormNode o where o.nodeId=" + nodeId;
		List<OFormNode> list = this.formDao.findByHql(hql1);
		String ids = "";
		for (OFormNode fn : list) {
			if (ids == "")
				ids = ids + fn.getProportyId();
			else {
				ids = ids + "," + fn.getProportyId();
			}
		}
		String hql2 = "from OFormProporty o where o.id in (" + ids + ")";

		return this.formDao.findByHql(hql2);
	}

	public String getFormProportyIdsByNode(int nodeId) {
		String hql1 = "from OFormNode o where o.nodeId=" + nodeId;
		List<OFormNode> list = this.formDao.findByHql(hql1);
		String ids = "";
		for (OFormNode fn : list) {
			if (ids == "")
				ids = ids + fn.getProportyId();
			else {
				ids = ids + "," + fn.getProportyId();
			}
		}
		return ids;
	}

	public List<OFormProporty> getMustBeFormProportyIdsByNode(int nodeId) {
		String hql1 = "from OFormNode o where o.nodeId=" + nodeId + "and o.mustBe=" + "0";
		List<OFormNode> list = this.formDao.findByHql(hql1);
		String ids = "";
		for (OFormNode fn : list) {
			if (ids == "")
				ids = ids + fn.getProportyId();
			else {
				ids = ids + "," + fn.getProportyId();
			}
		}
		String hql2 = "from OFormProporty o where o.id in (" + ids + ")";
		return this.formDao.findByHql(hql2);
	}

	public List<OFormProporty> getWriteFormProportyIdsByNode(int nodeId) {
		String hql = "from OFormProporty o where o.id in( select proportyId from OFormNode o where o.nodeId in(" + nodeId + "))";
		return this.formDao.findByHql(hql);
	}

	public String getFormProportyPageNames(List<OFormProporty> list) {
		String ids = "";
		if ((list != null) && (!list.isEmpty())) {
			for (OFormProporty fp : list) {
				if (ids == "")
					ids = ids + fp.getProportyPageName();
				else {
					ids = ids + "," + fp.getProportyPageName();
				}
			}
		}
		return ids;
	}

	public List<TemplateFile> getTemplateList() {
		String hql = "select recordid,filename from TemplateFile";
		List<Object[]> list = this.workflowManagerDao.findByHql(hql);
		List fileList = new ArrayList();
		for (Object[] obj : list) {
			TemplateFile file = new TemplateFile();
			file.setRecordid(obj[0].toString());
			file.setFilename(obj[1].toString());
			fileList.add(file);
		}
		return fileList;
	}

	public List<OFlowFormProporty> getWriteProportyList(String nodeId, OUser user) {
		String hql = "from OFlowFormProporty o where o.id in (select proportyId  from OFormNode  where nodeId in(" + nodeId + "))";
		List<OFlowFormProporty> proportyList = this.workflowManagerDao.findByHql(hql);
		for (OFlowFormProporty proporty : proportyList) {
			if ("org".equals(proporty.getType()))
				proporty.setDefaultValue(user.getOrgName());
			else if ("user".equals(proporty.getType())) {
				proporty.setDefaultValue(user.getName());
			}
		}
		return proportyList;
	}

	public synchronized Integer createProssinstance(String processinstanceId, int flowId, int nodeId, String executor, int userId, String message, String title) {
		OProcessinstance processinstance = null;
		ONode node = (ONode) this.workflowManagerDao.getObject(new ONode(), nodeId);
		if (node == null) {
			node = new ONode();
			node.setId(Integer.valueOf(0));
			node.setName("结束");
		}
		List nodeList = getNextNode(nodeId);
		if ((processinstanceId != null) && (!"".equals(processinstanceId))) {
			processinstance = (OProcessinstance) this.workflowManagerDao.getObject(new OProcessinstance(), Integer.parseInt(processinstanceId));
			processinstance.setNodeId(node.getId());
			if ((nodeList == null) || (nodeList.isEmpty()))
				processinstance.setNodeStatus("end");
			else {
				processinstance.setNodeStatus("task");
			}
			processinstance.setActorType(node.getActorType());
			if ("1".equals(node.getActorType())) {
				processinstance.setExecutor(getTaskActorAsArray(node.getId().intValue()));
				if ("1".equals(message))
					sendMessage(getTaskActorAsArray(node.getId().intValue()), title);
			} else if ("4".equals(node.getActorType())) {
				processinstance.setExecutor(processinstance.getUserId().toString());
				if ("1".equals(message))
					sendMessage(processinstance.getUserId().toString(), title);
			} else {
				processinstance.setExecutor(executor);
				if ("1".equals(message)) {
					sendMessage(executor, title);
				}
			}
			this.workflowManagerDao.updateOProcessinstance(processinstance);
		} else {
			processinstance = new OProcessinstance();
			processinstance.setExecutor(executor);
			processinstance.setFlowId(Integer.valueOf(flowId));
			processinstance.setNodeId(node.getId());
			if ((nodeList == null) || (nodeList.isEmpty()))
				processinstance.setNodeStatus("end");
			else {
				processinstance.setNodeStatus("task");
			}
			processinstance.setCeateTime(new Date());
			processinstance.setUserId(Integer.valueOf(userId));
			if ("1".equals(node.getActorType())) {
				processinstance.setExecutor(getTaskActorAsArray(node.getId().intValue()));
				if ("1".equals(message))
					sendMessage(getTaskActorAsArray(node.getId().intValue()), title);
			} else if ("4".equals(node.getActorType())) {
				processinstance.setExecutor(processinstance.getUserId().toString());
				if ("1".equals(message))
					sendMessage(processinstance.getUserId().toString(), title);
			} else {
				processinstance.setExecutor(executor);
				if ("1".equals(message)) {
					sendMessage(executor, title);
				}
			}
			this.workflowManagerDao.saveOProcessinstance(processinstance);
		}
		return processinstance.getId();
	}

	public void saveProssHis(int prossId, int nodeId, OUser user, String executor, String status) {
		ONode node = (ONode) this.workflowManagerDao.getObject(new ONode(), nodeId);
		if (node == null) {
			node = new ONode();
			node.setId(Integer.valueOf(0));
			node.setName("结束");
		}
		OProcessinstance processinstance = getProcessinstance(prossId);
		OProcessHis his = new OProcessHis();
		his.setCreateTime(new Date());
		his.setNodeId(node.getId());
		his.setProcessId(Integer.valueOf(prossId));
		his.setStatus(status);
		his.setActorType(node.getActorType());
		if ("0".equals(node.getStatus())) {
			his.setEndNode(node.getId());
			his.setEndNodeName(node.getName());
		} else {
			his.setStartNode(processinstance.getNodeId());
			his.setEndNode(node.getId());
			his.setEndNodeName(node.getName());
		}
		his.setExecutor(user.getName());
		his.setExecutorId(user.getId());
		if ("1".equals(node.getActorType()))
			his.setActors(getTaskActorAsArray(node.getId().intValue()));
		else if ("4".equals(node.getActorType()))
			his.setActors(processinstance.getUserId().toString());
		else {
			his.setActors(executor);
		}
		this.workflowManagerDao.saveProcessHis(his);
	}

	public synchronized void updateNextNode(OUser user, Map<String, String> map) {
		OWorkflow flow = getWorkflow(Integer.parseInt((String) map.get("flowId")));
		OFormDefinition definition = this.flowFormService.getFormDefinition(flow.getFormId().intValue());
		Integer instanceId = createProssinstance(null, Integer.parseInt((String) map.get("flowId")), Integer.parseInt((String) map.get("nodeId")),
				(String) map.get("exuctor"), user.getId().intValue(), (String) map.get("message"), flow.getName());
		if ((map.get("tableId") != null) && (!"".equals(map.get("tableId"))) && (!"0".equals(map.get("tableId")))) {
			map.put("instanceId", instanceId.toString());
			this.flowFormService.updateFormContent((String) map.get("tableId"), definition.getFormId().intValue(), map);
		} else {
			this.flowFormService.saveFormContent(definition.getFormId().intValue(), map, instanceId.intValue());
		}
		saveProssHis(instanceId.intValue(), Integer.parseInt((String) map.get("nodeId")), user, ((String) map.get("exuctor")).toString(), "开始");
	}

	public synchronized void updateNextNodeTransaction(OUser user, Map<String, String> map) {
		OWorkflow flow = getWorkflow(Integer.parseInt((String) map.get("flowId")));
		OFormDefinition definition = this.flowFormService.getFormDefinition(flow.getFormId().intValue());
		saveProssHis(Integer.parseInt((String) map.get("instanceId")), Integer.parseInt((String) map.get("nodeId")), user,
				((String) map.get("exuctor")).toString(), "");
		Integer instanceId = createProssinstance((String) map.get("instanceId"), Integer.parseInt((String) map.get("flowId")),
				Integer.parseInt((String) map.get("nodeId")), (String) map.get("exuctor"), user.getId().intValue(), (String) map.get("message"), flow.getName());
		this.flowFormService.updateFormContent((String) map.get("tableId"), definition.getFormId().intValue(), map);
		if ("904".equals(map.get("nodeId"))) {
			OProcessinstance instance = getProcessinstance(instanceId.intValue());
			Map leaveMap = this.flowFormService.getLeaveById((String) map.get("tableId"));
			this.holidayDao.saveLeave((String) leaveMap.get("days"), instance.getUserId().intValue(), (String) leaveMap.get("type"), instanceId.intValue());
		} else if ("257".equals(map.get("nodeId"))) {
			OProcessinstance instance = getProcessinstance(instanceId.intValue());
			Map dayMap = this.flowFormService.getHolidayId((String) map.get("tableId"));
			this.holidayDao.saveHoliday((String) dayMap.get("days"), instance.getUserId().intValue(), instanceId.intValue());
		}
	}

	public List<Integer> getOrgAndParent(int orgId) {
		String sql = "SELECT  lpad('',(LEVEL))||id FROM o_org  START WITH id=" + orgId + "  CONNECT BY PRIOR pid=id";
		List<String> list = this.workflowManagerDao.findBySql(sql);
		List array = new ArrayList();
		for (String id : list) {
			array.add(Integer.valueOf(Integer.parseInt(id.trim())));
		}
		return array;
	}

	public void saveSubject(Subject subject, OUser ou) {
		subject.setCreationTime(new Date());
		subject.setFounderUserid(ou.getId());
		subject.setUserName(ou.getName());
		this.formDao.saveForm(subject);
		String[] ids = subject.getOrgId().split(",");
		for (int i = 0; i < ids.length; i++) {
			SubjectOrg so = new SubjectOrg();
			so.setOrgId(Integer.valueOf(Integer.parseInt(ids[i])));
			so.setSubjectId(subject.getId());
			this.formDao.saveForm(so);
		}
	}

	public List<Subject> getSubjectList(String name, int currentPage, int userId) {
		String hql = "from Subject ";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " where content like '%" + name.trim() + "%'";
		}
		hql = hql + "order by creationTime desc";
		return this.formDao.findByPage(hql, currentPage);
	}

	public Long countSubject(String name, int userId) {
		String hql = "select count(id) from Subject ";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " where content like '%" + name.trim() + "%'";
		}
		return this.formDao.countTotalByHql(hql);
	}

	public void deleteSubject(String SubjectId) {
		String hql = "delete from Subject where id in(" + SubjectId + ")";
		this.formDao.executeHql(hql);
		hql = "delete from SubjectOrg where subjectId in(" + SubjectId + ")";
		this.formDao.executeHql(hql);
	}

	public Subject getSubject(int id) {
		return (Subject) this.formDao.getObject(new Subject(), id);
	}

	public void updateSubject(Subject subject) {
		subject.setCreationTime(new Date());
		this.formDao.updateForm(subject);
		String hql = "delete from SubjectOrg where subjectId=" + subject.getId();
		this.formDao.executeHql(hql);
		String[] ids = subject.getOrgId().split(",");
		for (int i = 0; i < ids.length; i++) {
			SubjectOrg s = new SubjectOrg();
			s.setOrgId(Integer.valueOf(Integer.parseInt(ids[i])));
			s.setSubjectId(subject.getId());
			this.formDao.saveForm(s);
		}
	}

	public List<Subject> getDispatcherList(int orgId) {
		List list = getOrgAndParent(orgId);
		String hql = " from Subject t where t.id in(select subjectId from SubjectOrg where orgId in(:list))";
		return this.formDao.findListByName(hql, new String[] { "list" }, new Object[] { list });
	}

	public WorkflowManagerDao getWorkflowManagerDao() {
		return this.workflowManagerDao;
	}

	public void setWorkflowManagerDao(WorkflowManagerDao workflowManagerDao) {
		this.workflowManagerDao = workflowManagerDao;
	}

	public FormDao getFormDao() {
		return this.formDao;
	}

	public void setFormDao(FormDao formDao) {
		this.formDao = formDao;
	}

	public FlowFormService getFlowFormService() {
		return this.flowFormService;
	}

	public void setFlowFormService(FlowFormService flowFormService) {
		this.flowFormService = flowFormService;
	}

	public MailService getMailService() {
		return this.mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.WorkflowManagerService JD-Core Version: 0.6.0
 */